
import sys,os
from PyQt5 import QtCore,QtGui
from PyQt5.Qt import *


class FileAdmin(QWidget):

    def __init__(self):
        super(FileAdmin,self).__init__()
        self.resize(600,400)
        self.path = None
        self.filename = 'd:/master'
        self.set_ui()

    def set_ui(self):
        self.model1 = QFileSystemModel()
        self.model1.setFilter(QDir.Dirs|QDir.NoDotAndDotDot)
        QDir.setCurrent(self.filename)
        currPath = QDir.currentPath()
        self.model1.setRootPath(currPath)

        self.treeview1 = QTreeView()
        self.treeview1.setModel(self.model1)
        for col in range(1,4):
            self.treeview1.setColumnHidden(col,True)
        self.treeview1.clicked.connect(self.initUI)

        self.treeview1.setRootIndex(self.model1.index(currPath))

        self.model2 = QStandardItemModel()
        self.treeview2 = QTreeView()
        self.treeview2.setModel(self.model2)
        self.treeview2.doubleClicked.connect(self.onDoubleClicked)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.treeview1,1)
        self.layout.addWidget(self.treeview2,3)
        self.setLayout(self.layout)


    def initUI(self,Qmodelidx):
        self.model2.clear()
        PathData = []
        filePath = self.model1.filePath(Qmodelidx)
        self.path = filePath
        PathDataName = self.model2.invisibleRootItem()
        PathDataSet = os.listdir(filePath)
        PathDataSet.sort()

        for Data in range(len(PathDataSet)):
            if os.path.isdir(filePath + '\\' + PathDataSet[Data]) == False:
                PathData.append(PathDataSet[Data])
            else:
                tmp = self.treeview1.currentIndex()
                self.treeview1.expand(tmp)
        for got in range(len(PathData)):
            fileInfo = QFileInfo(PathData[got])
            fileIcon = QFileIconProvider()
            icon = QtGui.QIcon(fileIcon.icon(fileInfo))
            gosData = QStandardItem(PathData[got])
            PathDataName.setChild(got,gosData)
            gosData.setIcon(QtGui.QIcon(icon))

    def onDoubleClicked(self,index):
        tmp = self.model2.itemFromIndex(index).text()
        file = os.path.join(self.path,tmp)
        os.startfile(file)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = FileAdmin()
    win.show()
    sys.exit(app.exec_())
